import { reactive, ref } from 'vue'
import { addRow } from './table.js'
import { JSONCopy } from '@components/js/utile.js'

export let disabled = ref(false)
export let dialogVisible = ref(false)
export let dialogForm = ref(null)
export let dialogFormData = reactive({
  name: '',
  sex: '',
  addr: '',
  aliasName: '',
})
export let dialogFormItems = reactive([
  { type: 'input', label: '姓名', value: 'name', disabled: disabled },
  { type: 'input', label: '别名', value: 'aliasName', disabled: disabled },
  { type: 'number', label: '年龄', value: 'sex', disabled: disabled },
  { type: 'input', label: '地址', value: 'addr', disabled: disabled },
])
export let dialogRules = reactive({})
export function dialogOk() {
  dialogForm.value
    .validate()
    .then(() => {
      if (dialogFormData.id) {
        addRow(JSONCopy(dialogFormData), 'edit')
      } else {
        addRow(JSONCopy(dialogFormData))
      }
      dialogCancel()
    })
    .catch(() => {
      console.log('不通过')
    })
}
export function dialogCancel() {
  dialogVisible.value = false
  dialogForm.value.resetFields()
}
export function setDialogForm(data) {
  Object.assign(dialogFormData, data)
}
